package cn.tedu.javagame.ball;

import cn.tedu.javagame.star.Star;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class BallPanel extends JPanel implements MouseMotionListener {
    Color bkColor = new Color(125, 255, 255);
    List<Ball> balls = new ArrayList<>();
    Board board = new Board();//挡板对象
    List<Brick> bricks = new ArrayList<>();
    int score = 0;
    public BallPanel() {
        this.setBackground(bkColor);
        // 将创建弹球对象的逻辑移到构造方法中
        for (int i = 0; i < 1; i++) {
            balls.add(new Ball());
        }
        for (int i = 0; i < 30;i++){
            bricks.add(new Brick());

        }
        int i=0,j=-1;
        for (Brick brick : bricks){
            if (i % 6 == 0){
                i=0;
                j++;
            }
            brick.setX((brick.width * i+ 10*i)+15);
            brick.setY(((brick.height*j) + 10*j));
            System.out.println("x:"+brick.x+",y:"+brick.y);
            i++;
        }
        this.addMouseMotionListener(this);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        for (Ball ball : balls) {
            g.setColor(ball.color);
            g.fillOval(ball.x, ball.y, ball.length, ball.length);
        }
        g.setColor(Color.BLACK);
        g.fillRect(board.x,board.y,board.width,board.height);
        int i = 0,j = -1;
        for (Brick brick : bricks){
            if (i % 6 == 0){
                i=0;
                j++;
            }
            g.setColor(brick.color);
            g.fillRect(brick.x, brick.y,brick.width,brick.height);
            g.setColor(Color.BLACK);
            g.drawString(score+"分",0,BallFrame.HEIGHT/2-20);
            i++;
        }
    }

    public void action() {
        java.util.Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                for (Ball b : balls) {
                    b.move();
                    board.catchBall(b);
                    for (int i = bricks.size() - 1; i >= 0; i--) {
                        Brick brick = bricks.get(i);
                        if (brick.catchBall(b)) {
                            bricks.remove(i);
                            System.out.println("移除砖块，剩余: " + bricks.size());
                            score++;
                            System.out.println("x: " + brick.x+" y: " + brick.y);
                        }
                    }

                }

                repaint();
            }
        }, 20, 10);
    }


    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        if (e.getX()-(board.width/2)<=0){
            board.move(0);
        }else if(e.getX()-(board.width/2) >= BallFrame.WIDTH- (board.width*1.15)){
            board.move((int)(580 -( board.width*1.15)));
        }else{
            board.move(e.getX()-(board.width/2));
        }


    }
}